package com.baikeyang.app.controller.admin;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DemoController {

	public static void main(String[] args) {
		BufferedImage bufferImg = null;
		try {

			File f = new File("e:/xxx.xlsx");
			if(f.exists()){
				f.delete();
			}

			ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
			bufferImg = ImageIO.read(new File("E:/照片/58b82cf1af8a48be81baaaf5dfc7ecad.jpg"));
			ImageIO.write(bufferImg, "jpg", byteArrayOut);

			List<List<?>> rows = new ArrayList<List<?>>();
			Random r = new Random();
			for(int i = 0; i < 100; i++){
				;
				List<?> row1 = CollUtil.newArrayList("aa"+i, "bb"+i, "cc"+i, "dd"+i, DateUtil.date(), r.nextDouble());
				rows.add(row1);
			}
			BigExcelWriter writer= ExcelUtil.getBigWriter("e:/xxx.xlsx", "表1");
			// 一次性写出内容，使用默认样式
			writer.write(rows);
			ExcelWriter e = writer.setSheet("表2");
			//画图的顶级管理器，一个sheet只能获取一个（一定要注意这点）
			Drawing patriarch = e.getSheet().createDrawingPatriarch();
			//anchor主要用于设置图片的属性
			XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8);
			anchor1.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);

			XSSFClientAnchor anchor2 = new XSSFClientAnchor(0, 0, 255, 255,(short) 1, 20, (short) 5, 34);
			anchor2.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);

			//插入图片
			patriarch.createPicture(anchor1, writer.getWorkbook().addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

			patriarch.createPicture(anchor2, writer.getWorkbook().addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

			// 关闭writer，释放内存
			writer.close();
		}catch (Exception e) {
			e.printStackTrace();
		}

	}

}
